﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MQuangCao
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
        public List<QuangCao> HienThiTrangChu_Trai()
        {
            var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri==1) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public List<QuangCao> HienThiTrangChu_Phai()
        {
            var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 2) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        //public List<QuangCao> HienThiTenNoiDungGiua()
        //{
        //    var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 4) orderby f.ThuTu descending select f;
        //    return v.ToList();
        //}
        //public List<QuangCao> HienThiGiuaNoiDungGiua()
        //{
        //    var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 5) orderby f.ThuTu descending select f;
        //    return v.ToList();
        //}
        //public List<QuangCao> HienThiDuoiNoiDungGiua()
        //{
        //    var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 3) orderby f.ThuTu descending select f;
        //    return v.ToList();
        //}
        public List<QuangCao> QuangCaoChayTrai()
        {
            var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 3) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public List<QuangCao> QuangCaoChayPhai()
        {
            var v = from f in context.QuangCaos.Where(d => d.HienThi == true && d.ViTri == 4) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public List<QuangCao> FindAll()
        {
            var v = from f in context.QuangCaos orderby f.ThuTu descending select f;
            return v.ToList();
        }

        public List<ViTriQuangCao> FindAll_ViTriQuangCao()
        {
            var v = from f in context.ViTriQuangCaos select f;
            return v.ToList();
        }
        public List<TargetQuangCao> FindAll_TargetQuangCao()
        {
            var v = from f in context.TargetQuangCaos select f;
            return v.ToList();
        }
        public List<LoaiQuangCao> FindAll_LoaiQuangCao()
        {
            var v = from f in context.LoaiQuangCaos select f;
            return v.ToList();
        }
        public int GetTotalQuangCao()
        {
            return FindAll().Count();
        }
        public int GetTotalQuangCaoTrai()
        {
            return FindAll().Where(d=>d.HienThi==true && d.ViTri==1).Count();
        }
        public int GetTotalQuangCaoPhai()
        {
            return FindAll().Where(d => d.HienThi == true && d.ViTri == 2).Count();
        }
        public void Save(QuangCao sp)
        {
            context.QuangCaos.InsertOnSubmit(sp);
            context.SubmitChanges();
        }
        public void Update(QuangCao sp)
        {
            var v = from f in context.QuangCaos
                    select f;
            QuangCao spdb = v.SingleOrDefault(d => d.MaQuangCao.Equals(sp.MaQuangCao));           
            spdb.HinhAnh = sp.HinhAnh;
            spdb.Link = sp.Link;           
            spdb.ThuTu = sp.ThuTu;
            spdb.ViTri = sp.ViTri;
            spdb.Target = sp.Target;
            spdb.LoaiQuangCao = sp.LoaiQuangCao;
            context.SubmitChanges();
        }

        public void Delete(int id)
        {
            var v = from p in context.QuangCaos
                    where p.MaQuangCao.Equals(id)
                    select p;
            QuangCao spdb = v.SingleOrDefault();
            context.QuangCaos.DeleteOnSubmit(spdb);
            context.SubmitChanges();
        }
        public void SaveChange()
        {
        }
        public void HienThi(int id)
        {
            var v = from f in context.QuangCaos
                    select f;
            QuangCao spdb = v.SingleOrDefault(d => d.MaQuangCao.Equals(id));
            if (spdb.HienThi == true)
            {
                spdb.HienThi = false;
            }
            else spdb.HienThi = true;
            context.SubmitChanges();
        }
        public QuangCao GetByID(int id)
        {
            return (FindAll().Where(d => d.MaQuangCao == id)).SingleOrDefault();
        }


        public int GetMax()
        {
            return (from t in context.QuangCaos
                    orderby t.MaQuangCao descending
                    select t.MaQuangCao).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.QuangCaos
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
    }
}